package cn.net.withub.dataCollector.fastdfs.utils;

import cn.net.withub.dataCollector.common.exception.AppException;
import cn.net.withub.dataCollector.fastdfs.*;
import cn.net.withub.dataCollector.fastdfs.model.NameValuePair;
import cn.net.withub.dataCollector.fastdfs.service.FileManager;
import cn.net.withub.dataCollector.fastdfs.service.FileManagerConfig;
import cn.net.withub.dataCollector.common.utils.CommonProperties;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.io.*;

@SuppressWarnings({"unused","resource"})
@Component(value="fastDfsUtil")
public class FastDfsUtil {
	private static Logger log = Logger.getLogger(FastDfsUtil.class);

	/**
	 * fds上传
	 * @param wjlj 文件物理路径
	 */
	public static String Upload(String wjlj) throws Exception{

		File f = new File(wjlj);
		String filePath = "";
		String serverFilePath = "";
		if (f.exists()) {
			String fileExtName = wjlj.substring(wjlj.lastIndexOf(".") + 1);
			String filename = f.getName();

			ClientGlobal.init(CommonProperties.getClassPath()+"/fdfs_client.conf");
			// 创建一个TrackerClient对象。
			TrackerClient trackerClient = new TrackerClient();
			// 创建一个TrackerServer对象。
			TrackerServer trackerServer = trackerClient.getConnection();
			// 声明一个StorageServer对象，null。
			StorageServer storageServer = null;
			// 获得StorageClient对象。
			StorageClient storageClient = new StorageClient(trackerServer, storageServer);
			// 直接调用StorageClient对象方法上传文件即可。
			String[] strings;

			NameValuePair[] meta_list = new NameValuePair[4];
			meta_list[0] = new NameValuePair("fileName", filename);
			meta_list[1] = new NameValuePair("fileLength", String.valueOf(f.length()));
			meta_list[2] = new NameValuePair("fileExt", fileExtName);
			meta_list[3] = new NameValuePair("fileAuthor", FileManagerConfig.FILE_DEFAULT_AUTHOR);

			strings = storageClient.upload_file(wjlj, fileExtName, null);

			String groupName = strings[0];
			String remoteFileName = strings[1];

			serverFilePath = FileManagerConfig.PROTOCOL
					+ ClientGlobal.TRACKER_NGNIX_ADDR
					//+ trackerServer.getInetSocketAddress().getHostName()
					//+ SEPARATOR + TRACKER_NGNIX_PORT
					+ FileManagerConfig.SEPARATOR + groupName
					+ FileManagerConfig.SEPARATOR + remoteFileName;

			f.delete();
		}else{
			throw new AppException(wjlj+" 该文件路径不存在文件...");
		}

		return serverFilePath;
	}

	/**
	 * fds下载
	 */
	public byte[] Download(String filePath)  throws Exception{
		String substr = filePath.substring(filePath.indexOf("group"));
		String group = substr.split("/")[0];
		String remoteFileName = substr.substring(substr.indexOf("/") + 1);
		String specFileName = substr.substring(substr.indexOf("."));
		try {
			byte[] b = FileManager.download(group, remoteFileName, specFileName);

			return b;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String ReadText(String wjlj) {
		String result = "";
		File file = new File(wjlj);
		InputStreamReader isr = null;
		BufferedReader bufferedReader = null;

		try {
			isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
			bufferedReader = new BufferedReader(isr);
			try {
				String read = "";
				while ((read = bufferedReader.readLine()) != null) {
					result += read + "\r\n";
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (bufferedReader != null) {
					bufferedReader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return result;
	}
}
